//事件监听
class listener {

    constructor () {
        this.handlers = {};
    }

    on(eventType, handler) {
        if (!(eventType in this.handlers)) {
            this.handlers[eventType] = [];
        }
        this.handlers[eventType].push(handler);
        return this;
    }

    off(eventType, handler) {
        if (eventType) {
            if (typeof handler === "function") {
                let i = this.handlers[eventType].findIndex(f => f === handler);
                this.handlers[eventType].splice(i, 1);
            } else {
                this.handlers[eventType] = [];
            }
        } else {
            Object.keys(this.handlers).forEach(key => {
                this.handlers[key] = [];
            });
        }
        return this;
    }

    emit(eventType) {
        let handlerArgs = Array.prototype.slice.call(arguments, 1);
        if (!this.handlers || !this.handlers[eventType]) return this;
        this.handlers[eventType].forEach(handler => {
            handler.apply(this, handlerArgs);
        });
        return this;
    }
}

const getSize = ( maxWidth = 640 ) => {
    const { innerWidth , innerHeight } = window;
    const width = innerWidth < maxWidth ? innerWidth : maxWidth;
    return { width:width, height:innerHeight }
}


export {
    listener,
    getSize
}